package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bo.AppBO;
import com.jms.fu.model.bo.AppDownBO;

public class GetAppListAction extends Action {
	private static Logger logger = Logger.getLogger(LoginAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		String role = (String) session.getAttribute("role");
		String cat = request.getParameter("category");
		String sortby = request.getParameter("sortby");
		String pricefilter = request.getParameter("pricefilter");
		int numberinpage;
		int pagenumber;
		if (request.getParameter("numberinpage") == null) {
			numberinpage = 12;
		} else {
			numberinpage = Integer.parseInt(request
					.getParameter("numberinpage"));
		}
		if (request.getParameter("pagenumber") == null) {
			pagenumber = 1;
		} else {
			pagenumber = Integer.parseInt(request.getParameter("pagenumber"));
		}
		if (sortby == null) {
			sortby = "dateUpload";
		}
		if (cat == null) {
			cat = "all";
		}
		if (pricefilter == null) {
			pricefilter = "all";
		}

		AppBO appbo = new AppBO();
		ArrayList<App> listApp = new ArrayList<App>();

		try {
			listApp = appbo.getListApp(cat, sortby, pricefilter, numberinpage,
					pagenumber);
			String applistHTML = "";
			AppDownBO appdown = new AppDownBO();
			for (App temp : listApp) {
				boolean check;
				if(username!=null){
				check = ((username.equals(temp.getDevId())) || (role
						.equals("Administrator")));
				}else{
					check = false;
				}
				ArrayList<String> avatar = appbo.getNumberOfFile(getServlet().getServletContext().getRealPath("/")+temp.getLink()+"/Avatar");
				String avatarLink;
				if(avatar.size()==0){
					avatarLink = "references/images/avatar.jpg"; 
				}else{
					avatarLink = temp.getLink()+"/Avatar/"+avatar.get(0);
				}
				
				boolean checkGetLink = false;
				String linkDownload = "";
				if(role!=null||username!=null){
					if (temp.getPrice() == 0 || appdown.checkAppOwnByUsername(username, temp.getAppId())
							|| temp.getDevId().equals(username)
							|| role.equals("Administrator")) {
						String appname = appbo.getNumberOfFile(
								getServlet().getServletContext().getRealPath("/")
										+ temp.getLink()).get(0);
						linkDownload = temp.getLink() + "/" + appname;
						checkGetLink=true;
					}
				}
				
				applistHTML +="<div class='image_grid portfolio_8col'>"
						+ "<ul id='list' class='portfolio_list da-thumbs'><li>"
						+ "<img src='"+avatarLink+"'>"
								+ "<article class='da-animate da-slideFromTop' "
								+ "style='display: block;'> "
								+ "<h3 class='price'>"+(temp.getPrice() > 0 ? (temp.getPrice()+"$") : "Free")+"</h3> "
												+ (!checkGetLink?"<span class='link_buy'><a id='btn-buy-"+temp.getAppId()+"'  onclick='buyItem("+temp.getAppId()+")' title='Buy'></a></span>":
													"<span class='link_buy'><a id='btn-buy-"+temp.getAppId()+"' href='"+linkDownload+"' title='Download'></a></span>")
												+ "<span class='link_post'><a  onclick='addToCart("+temp.getAppId()+")' title='Add To Cart'></a></span>"
												+ "<span class='link_see'><a href='getAppDetail.jms?appid="+temp.getAppId()+"' title='See more'></a></span>"
												+ (check?"<span class='link_share'><a onclick='deleteAppAction("+temp.getAppId()+")' title='delete'></a></span>"
												+ "<span class='link_rate'><a href='' data-toggle='modal' data-target='#myModal'  onclick='getUpdateForm("+temp.getAppId()+")'  title='update'></a></span>":"")
												+ "</article>"
												+ "<div><a class='item-g'>"+temp.getAppName()+"</a></div>"
														+ "</li></ul></div>";
			}
			PrintWriter out = response.getWriter();
			out.print(applistHTML);
			out.close();
			return null;
		} catch (DAOException | IOException e) {
			logger.error(e.getMessage());
			return mapping.findForward("failed");
		} 
	}
}
